package cn.lingyangwl.agile.pay.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringReader;
import java.io.StringWriter;

/**
 * Velocity工具类,根据模板内容生成文件
 *
 * @author shenguangyang
 */
@Slf4j
public class VelocityUtil {
    static {
        Velocity.init();
    }

    public static String generate(VelocityContext context, String template) {
        try (StringReader reader = new StringReader(template);
             StringWriter writer = new StringWriter()) {
            // 不用vm文件
            Velocity.evaluate(context, writer, "mystring", reader);
            return writer.toString();
        } catch (Exception e) {
            log.error("error: {}", e.getMessage());
        }
        return "";
    }
}
